package org.iks.push.feign;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.iks.common.AjaxResult;
import org.iks.common.ResultCode;
import org.iks.common.constant.Constant;
import org.iks.common.util.ServletUtil;
import org.iks.common.pojo.message.ChatMessage;
import org.iks.push.serial.SerializableType;
import org.iks.push.business.controller.PushController;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * Author kzou
 * Date 2024/1/30
 * Description:
 */
@Slf4j
@Service
public class PushFeignRemoteService {

    @Value("${server.servlet.context-path:}")
    private String contextPath;

    /**
     * @param address     ip:port
     * @param chatMessage meg
     */
//    public AjaxResult<?> delegateMessage(String address, ChatMessage chatMessage) {
//        if (chatMessage != null && !contextPath.isEmpty()) {
//            address = address + contextPath;
//        }
//        address = address + "/chatMsg";
//        address = address.replace("//", "/");
//        byte[] data = SerializableType.JSON.serialize(chatMessage);
//        if (data == null) {
//            log.warn("cant not delegate empty message to {}", address);
//            return AjaxResult.error(ResultCode.RPT_ERROR);
//        }
//        // send rpc
//        HttpRequest httpRequest = HttpRequest.post(address);
//        HttpResponse response = httpRequest
//                .body(data)
//                .header(Constant.AUTHENTICATION, ServletUtil.getToken())
//                .execute(false);
//        byte[] responseData = response.bodyBytes();
//        ObjectMapper objectMapper = new ObjectMapper();
//        try {
//            return objectMapper.readValue(responseData, AjaxResult.class);
//        } catch (IOException e) {
//            log.warn("cant not deserializable {}", responseData);
//            return AjaxResult.error(ResultCode.RPT_ERROR);
//        }
//    }

}
